package com.Service.Impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.Dao.BaseDaoI;
import com.Entity.Userdata;
import com.Entity.Userhealthdata;
import com.Entity.Users;
import com.Service.UserHealthDataService;

@Service("userHealthDataService")
public class UserHealthDataServiceImpl extends BaseServiceImpl<Userhealthdata> implements UserHealthDataService {
	@Autowired
	private BaseDaoI<Userhealthdata> userhealthdatadao;
	List<Userhealthdata> userhealthdatalist=new ArrayList<Userhealthdata>();

	
	@Override
	public List<Userhealthdata> findByUserId(int userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		
		String hql = "from Userhealthdata t where t.userId = :userId";
		try {
			userhealthdatalist=this.userhealthdatadao.find(hql, params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userhealthdatalist;
	}
	
	@Override
	public List<Userhealthdata> findByUserIdDesc(int userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		
		String hql = "from Userhealthdata t where t.userId = :userId order by t.dataUploadTime DESC";
		try {
			userhealthdatalist=this.userhealthdatadao.find(hql, params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userhealthdatalist;
	}
	
	@Override
	public List<Userhealthdata> findByDoctorId(int doctorId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("doctorId", doctorId);
		
		String hql = "from Userhealthdata t where t.doctorId = :doctorId order by t.dataUploadTime DESC";
		try {
			userhealthdatalist=this.userhealthdatadao.find(hql, params);
			System.out.println("doctorId:"+doctorId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userhealthdatalist;
	}
	
	@Override
	public List<Userhealthdata> findOrders(int doctorId,short orderState) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("doctorId", doctorId);
		params.put("orderState", orderState);
		String hql = "from Userhealthdata t where t.doctorId = :doctorId and t.orderState = :orderState " +
				"order by t.dataUploadTime DESC";
		try {
			userhealthdatalist=this.userhealthdatadao.find(hql, params);
			System.out.println("doctorId:"+doctorId);
			System.out.println("orderState:"+orderState);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userhealthdatalist;
	}
	
	@Override
	public List<Userhealthdata> findOrders(short orderState) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderState", orderState);
		String hql = "from Userhealthdata t where t.orderState = :orderState order by t.dataUploadTime DESC";
		try {
			userhealthdatalist=this.userhealthdatadao.find(hql, params);
			System.out.println("orderState:"+orderState);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userhealthdatalist;
	}
	
	@Override
	public List<Userhealthdata> findByTransfer(short transferState) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("transferState", transferState);
		String hql = "from Userhealthdata t where t.transferState = :transferState order by t.dataUploadTime DESC";
		try {
			userhealthdatalist=this.userhealthdatadao.find(hql, params);
			System.out.println("transferState:"+transferState);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userhealthdatalist;
	}
	
	@Override
	public List<Userhealthdata> findDataWithOrders(int userId, short orderState) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("orderState", orderState);
		String hql = "from Userhealthdata t where t.userId = :userId and t.orderState = :orderState " +
				"order by t.dataUploadTime DESC";
		try {
			userhealthdatalist=this.userhealthdatadao.find(hql, params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userhealthdatalist;
	}
	
	@Override
	public List<Userhealthdata> getHealthdataListByUserId(int UserID,int page,int rows) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userID", UserID);
		String hql="from Userhealthdata t where t.userId=:userID order by dataUploadTime DESC";
		return userhealthdatadao.find(hql,params, page, rows);
	}
	@Override
	public List<Userhealthdata> getBloodPressureByUserId(int UserID) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userID", UserID);
		String sql="select new Userhealthdata(diastolicPressure,systolicPressure) from Userhealthdata t where t.userId=:userID order by dataUploadTime DESC";
		return userhealthdatadao.find(sql,params);
	}

}
